package com.zzptc.zzsgz.bdsjws.services;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import java.io.File;
import okhttp3.Call;
import okhttp3.Request;

public class MyDownLoadService extends Service {
    NotificationCompat.Builder notification;
    NotificationManager notificationManager;
    File file;
    public MyDownLoadService (){

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        String url = null;
        if (intent != null){
            url = intent.getStringExtra("apkurl");
        }
        createNotification();
        Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
        MyUtil myUtil = new MyUtil();
        file = myUtil.getDir();
        downloadfile(url , file);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void downloadfile(String url , final File file){
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(file.getAbsolutePath() , "hhhh.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        notification.setContentTitle("下载失败");
                        notificationManager.notify(0 , notification.build());
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        notification.setContentText("下载完毕");
                        notification.setAutoCancel(true);
                        notificationManager.notify(0,notification.build());

                        //安装apk 方法
                        installApk(response);
//                        Intent intent = MyUtil.getInstallintent(file);
//                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyDownLoadService.this);
//                        stackBuilder.addParentStack(UpdateActivity.class);
//                        stackBuilder.addNextIntent(intent);
//                        PendingIntent resultPendingIntent =
//                                stackBuilder.getPendingIntent(
//                                        0,
//                                        PendingIntent.FLAG_CANCEL_CURRENT
//                                );
//                        notification.setContentInfo(resultPendingIntent);

                        //让进度条消失
//                        notification.setProgress(0,0,false);
//                        notification.setContentText("请点击安装");
//                        notification.setContentTitle("下载完毕");
//                        notificationManager.notify(0 , notification.build());
                    }


                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        notification.setProgress(100 , (int)(progress*100),false);
                        notificationManager.notify(id,notification.build());
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                    }

                });

    }

    //安装apk   方法
    private void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
        MyApplication.getContext().startActivity(intent);
    }


    //创建通知栏
    public void createNotification(){
        notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(NOTIFICATION_SERVICE);
        notification = new NotificationCompat.Builder(MyApplication.getContext());
        notification.setSmallIcon(R.mipmap.logo_small) //小图标
                .setContentText("正在下载")        //内容
                .setContentTitle("百度卫士下载")  //标题
                .build();
        notificationManager.notify(0,notification.build());
    }


}
